package com.budwk.app.sqfwV2.generator.dayingRoadPdf;

import com.alibaba.fastjson.JSONObject;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfwDoc.codeGenerator.pdfGenerate.BaseTemplatePdfGenerator;
import com.budwk.app.sqfwDoc.codeGenerator.pdfGenerate.SqfwPdfCreateCommonBO;
import com.budwk.app.sqfwV2.component.DayingRoadPdfComponent;
import com.budwk.app.sqfwV2.dto.dto.FireSafetyOnDayingRoadPdfDTO;
import com.budwk.app.sqfwV2.dto.dto.centuryCity.SafeCheckTaskParamDataDTO;
import com.budwk.app.sqfwV2.enums.dayingRoad.OtherPatrolContentsEnum;
import com.budwk.app.util.StrUtil;
import com.budwk.app.util.poiUtils.PictureUtils;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.xwpf.NiceXWPFDocument;
import lombok.extern.slf4j.Slf4j;

import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.Map;

/**
 * 瓶装液化燃气用户安全检查登记表，版本号1.0
 */

@Slf4j
public class BottleGasSafetyPdfGenerator extends BaseTemplatePdfGenerator{

    /**
    * 判断使用使用这个generator生成pdf
    *   ○【用火用电情况】状态中包含了【瓶装液化气】、【瓶装天然气】、【瓶装二甲醚】三项任意一项时，生成文档。
     *
    * @param pdfDTO 创建pdf数据素材
    * @return 结果
    */
    @Override
    public boolean pdfCreateConditionCheck(SqfwPdfCreateCommonBO pdfDTO) {
        log.info("BottleGasSafetyPdfGenerator pdfCreateConditionCheck start param: {}", JSONObject.toJSONString(pdfDTO));
        return DayingRoadPdfComponent.isGeneratePdf("1", pdfDTO);
    }

    @Override
    public String getTemplateName() {
        return "sqfwV2/daYingLu/bottleGasSafetyRegistration.docx";
    }

    @Override
    public void customerPopularPdfParam(Map<String, Object> paramMap, SqfwPdfCreateCommonBO pdfDTO) {
        // 1 获取FireSafetyOnDayingRoadPdfDTO对象
        FireSafetyOnDayingRoadPdfDTO roadPdfDTO = DayingRoadPdfComponent.getFireSafetyOnDayingRoadPdfDTO(paramMap, pdfDTO);
        // 2 将其余巡查内容转为map存贮
        List<SafeCheckTaskParamDataDTO> others = roadPdfDTO.getOtherPatrolContents();
        if (others == null || others.size() == 0) {
            others = OtherPatrolContentsEnum.getAllDefaults();
        }
        for (SafeCheckTaskParamDataDTO other : others) {
            paramMap.put(other.getFields(), DayingRoadPdfComponent.getProblem(other));
        }
        List<SafeCheckTaskParamDataDTO> otherHiddenDangers = roadPdfDTO.getOtherHiddenDangers();
        paramMap.put("otherHiddenDangers", "");
        if (otherHiddenDangers != null && otherHiddenDangers.size() > 0) {
            SafeCheckTaskParamDataDTO s = otherHiddenDangers.get(otherHiddenDangers.size() - 1);
            paramMap.put("otherHiddenDangers", s.getRectificationMeasure());
        }

        // 这里需要改动 暂时不确定该值
        paramMap.put("area", "");
        // 整改意见
        paramMap.put("rectificationContent", StrUtil.formatStr(roadPdfDTO.getRectificationContent()));
        // 3 构建巡查人员
        DayingRoadPdfComponent.buildPeersSign(pdfDTO, roadPdfDTO, paramMap);
        log.info("BottleGasSafetyPdfGenerator customerPopularSpecialParam resmap: {}", paramMap);
    }

    @Override
    public ByteArrayOutputStream generatePDFAfter(XWPFTemplate xwpfTemplate, Sqfw_formSummary_new formSummary) {
        if (!DayingRoadPdfComponent.isDayYingStreet(formSummary.getUnitName())) {
            return null;
        }
        ByteArrayOutputStream wordOutPutStream = new ByteArrayOutputStream();
        if (xwpfTemplate != null) {
            NiceXWPFDocument xwpfDocument = xwpfTemplate.getXWPFDocument();
            try {
                xwpfDocument = PictureUtils.localImageSeal(xwpfDocument, "排查人员", PictureUtils.getLocalImagePath(formSummary.getUnitName()),
                        20, -53, 115);
                xwpfDocument.write(wordOutPutStream);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                PictureUtils.close(xwpfTemplate, xwpfDocument);
            }
        }
        return wordOutPutStream;

    }
}
